﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
using System.Web;

namespace Card.Security
{
    public class Passport
    {
        private int _UserID = 0;

        public int UserID
        {
            get { return _UserID; }
            set { _UserID = value; }
        }
        private string _UserName = string.Empty;

        public string UserName
        {
            get { return _UserName; }
            set { _UserName = value; }
        }

        private string _Password = string.Empty;

        public string Password
        {
            get { return _Password; }
            set { _Password = value; }
        }


        public bool IsLogin
        {
            get
            {
                return this.UserID > 0;
            }
        }

        public override string ToString()
        {
            string strUserID = Utility.EncryptString(this.UserID.ToString());            
            string strUserName = Utility.EncryptString(this.UserName);
            string strPassword = Utility.EncryptPassWord(this.Password);

            return string.Format("{0},{1},{2}", strUserID, strUserName, strPassword);
        }

        public static Passport Parse(string strPassport)
        {
            Passport passport = new Passport();
            string[] strValues = strPassport.Split(new char[] { ',' });
            if (strValues.Length == 3)
            {
                for (int i = 0; i < strValues.Length; i++)
                {
                    if (!string.IsNullOrEmpty(strValues[i]))
                    {
                        if (i == 2)
                        {
                            strValues[i] = Utility.DecryptPassWord(strValues[i]);
                        }
                        else
                        {
                            strValues[i] = Utility.DecryptString(strValues[i]);
                        }
                    }
                }
                passport.UserID = int.Parse(strValues[0]);                

                passport.UserName = strValues[1];

                passport.Password = strValues[2];

            }
            return passport;
        }

        public static Passport Parse(BAF.Security.Passport passport)
        {
            Passport _passport = new Passport();
            if (passport != null)
            {
                _passport.UserID = _passport.UserID;
                _passport.UserName = _passport.UserName;                
            }

            return _passport;
        }
    }
}
